﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace B2B.Models
{
    public class SP_DanhSachSanPham
    {

        private int maSanPham;
        public int MaSanPham
        {
            get { return maSanPham; }
            set { maSanPham = value; }
        }

        private string tenSanPham;
        public string TenSanPham
        {
            get { return tenSanPham; }
            set { tenSanPham = value; }
        }

        private string hinhAnh;
        public string HinhAnh
        {
            get { return hinhAnh; }
            set { hinhAnh = value; }
        }

        private double gia;
        public double Gia
        {
            get { return gia; }
            set { gia = value; }
        }

        private int soLuongDatToiThieu;
        public int SoLuongDatToiThieu
        {
            get { return soLuongDatToiThieu; }
            set { soLuongDatToiThieu = value; }
        }      

        private string tenDVT;
        public string TenDVT
        {
            get { return tenDVT; }
            set { tenDVT = value; }
        }


    //    public ArrayList DanhSachSanPham(int page)

        private string loaiThanhToan;

        public string LoaiThanhToan
        {
            get { return loaiThanhToan; }
            set { loaiThanhToan = value; }
        }
        private int tinhTrang;

        public int TinhTrang
        {
            get { return tinhTrang; }
            set { tinhTrang = value; }
        }

      

        public ArrayList DanhSachSanPham(int page, out int tongPage)
        {
            tongPage = 0;
            int tongsp = 0;
            ArrayList list_SanPham = new ArrayList();
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_DANHSACHSANPHAM", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PAGE", page);           
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                SP_DanhSachSanPham sp = new SP_DanhSachSanPham();
                sp.MaSanPham = reader.GetInt32(0);
                sp.TenSanPham = reader.GetString(1);
                sp.HinhAnh = reader.GetString(2);
                sp.Gia = double.Parse(reader.GetDecimal(3).ToString());
                sp.SoLuongDatToiThieu = reader.GetInt32(4);        
                sp.TenDVT = reader.GetString(5);
                sp.LoaiThanhToan = reader.GetString(6);
                sp.TinhTrang = reader.GetInt32(7);
                list_SanPham.Add(sp);
            }
            if (reader.NextResult())
            {
                reader.Read();
                tongsp = reader.GetInt32(0);
                tongPage = tongsp / 10;
                if (tongsp % 10 != 0)
                    tongPage += 1;
            }

            conn.Close();
            return list_SanPham;
        }

        public ArrayList DanhSachSanPhamUserQuanLy(int page, out int tongPage, int maTV)
        {
            tongPage = 0;
            int tongsp = 0;
            ArrayList list_SanPham = new ArrayList();
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_DANHSACHSANPHAM_UserQuanLy", conn);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PAGE", page);
            cmd.Parameters.AddWithValue("MATV", maTV);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                SP_DanhSachSanPham sp = new SP_DanhSachSanPham();
                sp.MaSanPham = reader.GetInt32(0);
                sp.TenSanPham = reader.GetString(1);
                sp.HinhAnh = reader.GetString(2);
                sp.Gia = double.Parse(reader.GetDecimal(3).ToString());
                sp.SoLuongDatToiThieu = reader.GetInt32(4);
                sp.TenDVT = reader.GetString(5);
                sp.LoaiThanhToan = reader.GetString(6);
                sp.TinhTrang = reader.GetInt32(7);
                list_SanPham.Add(sp);
            }
            if (reader.NextResult())
            {
                reader.Read();
                tongsp = reader.GetInt32(0);
                tongPage = tongsp / 10;
                if (tongsp % 10 != 0)
                    tongPage += 1;
            }

            conn.Close();
            return list_SanPham;
        }

        public ArrayList DanhSachSanPhamTheoDanhMuc(int maDanhMuc, int page, out int tongpage)
        {
            int tong = 0;
            tongpage = 0;
            ArrayList list_SP = new ArrayList();
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_DANHSACHSANPHAMTHEODANHMUC",conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MADANHMUC", maDanhMuc);
            cmd.Parameters.AddWithValue("@PAGECURENT",page);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                SP_DanhSachSanPham sp = new SP_DanhSachSanPham();
                sp.MaSanPham = reader.GetInt32(0);
                sp.TenSanPham = reader.GetString(1);
                sp.HinhAnh = reader.GetString(2);
                sp.Gia = double.Parse(reader.GetDecimal(3).ToString());
                sp.SoLuongDatToiThieu = reader.GetInt32(4);
                sp.TenDVT = reader.GetString(5);
               
                list_SP.Add(sp);
            }
            if (reader.NextResult())
            {
                if (reader.Read())
                {
                    tong = reader.GetInt32(0);
                }
                tongpage = tong / 10;
                if (tong % 10 != 0) { tongpage += 1; }
            }
            conn.Close();
            return list_SP;
        }

        public int TongSoSanPham(int maDanhMuc)
        {
            int tong = 0;           
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_TongSanPhamTheoDanhMuc", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MADANHMUC", maDanhMuc);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                tong = reader.GetInt32(0);
            }
            int sotrang = tong / 10;
            if (tong % 10 != 0) { sotrang += 1; }
            return sotrang;
        }
       
    }
       
    
}